home *** CD-ROM | disk | FTP | other *** search
- ┌───────────────────────┐
- │ │
- │ RAND │
- │ August 27, 1992 │
- │ │
- │ By Ray Sun │
- │ │
- └───────────────────────┘
-
- Files included: RAND.PAS - Source code for random unit
- RAND.TPU - Turbo Pascal 6 unit for random numbers
- TYPETEST.PAS - Example for rand unit
- TYPETEST.EXE - Compiled example for rand unit
- RAND.DOC - Documentation for random unit
-
- Purpose: RAND.PAS provides good random numbers for pascal users. The
- numbers provided should show a statistically acceptable amount of runs
- and the average of the numbers should approach half of the input
- integer i. I credit a book of whose name I unfortunately cannot
- remember for the algorithm used for the rectangularly distributed
- random numbers. I merely implemented the algorithm as a pascal unit
- for the public to use.
-
- -----------------------------------------------------------------------
- Why RAND?
-
- The random numbers provided by Turbo Pascal sometimes have a
- tendency to "run," or provide the same "random" number over and over
- in succession. Also, patterns may develop in the numbers. This is
- noticeable in simple programs that rely completely on Pascal's Random
- function. For example, a friend of mine programmed a Spades game in
- pascal, but the game was not fair because each player would receive
- long runs in one suit and the overall distribution of cards was bad.
- For example, one player would receive all the cards from 4 to Queen in
- spades, and he would dominate the game.
-
- So why RAND?
-
- The RAND unit's random numbers do not have an unacceptable number
- of runs, nor is it short of runs. The numbers should show a
- statistically acceptable number of runs (Near the mean). Therefore,
- the numbers the user gets -should- be more random than the random
- numbers Turbo Pascal generates.
- -----------------------------------------------------------------------
- How do I use RAND?
-
- Simple! Just type:
-
- Uses rand;
-
- in your pascal source code with the RAND.TPU file accessible by Turbo
- Pascal. Then, in your main program block, include the following
- lines:
-
- BEGIN
- Initialize;
- ...
- ...
- ...
- x := RandomNumber(i);
- END.
-
- YOU MUST INCLUDE THE INITIALIZE LINE, OR THE RandomNumber FUNCTION
- WILL NOT WORK!
-
- If Initialize conflicts with the name of one of your current
- procedures, you may rename it to something more convenient, like
- InitializeRandom. The variable x used here may be any variable, as
- long as it is defined as any integer type. The number returned to x
- will be a number from 1 to i, where i is the integer input from the
- user.
-
- Example:
-
- Uses rand;
-
- Var z : integer;
-
- BEGIN
- Initialize;
- For loop := 1 to 1000 do
- begin
- z := RandomNumber(100);
- Writeln(z);
- end;
- END.
-
- The output here should be 1000 numbers between 1 and 100, hopefully
- random.
-
- If a further example is needed, see TYPETEST.PAS for a sample program
- which integrates this unit.
- -----------------------------------------------------------------------
- Problems and Questions
-
- Are you getting the same number over and over again?
-
- * You probably did not use Initialize; in your source code!
-
- Are you getting too many or not enough runs?
-
- * NEVER include Initialize; inside your loop! Initialize should be
- * one of your first source code lines within the main block, and it
- * should stand by itself. It should never be run more than once, or the
- * purpose of the RAND program is defeated!
-
- I need a real number between 0 and i instead of an integer. How can I
- do this?
-
- * Edit the source code RAND.PAS so that the final statement in the
- * Function Randomnumber definition reads:
- *
- * RandomNumber:=(frac(x/30269.0+y/30307.0+z/30323.0))*i;
- *
- * (without the * of course!)
-
- These numbers aren't random! What's the deal????
-
- * Can't help you on this one... I think it works, but if not, I just
- * dunno...
-
- RAND documentation completed.
- Typed by Ray Sun
- Program and Source Code by Ray Sun
-